Skip to content

Custom AI keys: keep the result when the keyboard hides, insert it only into the same field - #889

Open
4di-hello wants to merge 1 commit into
LeanBitLab:mainfrom
4di-hello:fix/ai-key-result-after-hide
Open

4di-hello wants to merge 1 commit into
LeanBitLab:mainfrom
4di-hello:fix/ai-key-result-after-hide

Conversation

@4di-hello

Copy link
Copy Markdown
Contributor

Some editors hide the keyboard while a custom AI key is running, e.g. because they react to the cursor being moved for the result (seen in Nine Mail). The request was cancelled and the result was lost.

  • Result kept: the request keeps running when the keyboard hides. A result that arrives meanwhile is inserted when the keyboard comes back into the same field within two minutes.
  • Only the same field: many apps use the same field id for all their text fields (e.g. every chat). So the field must also be in the same app, have the same input type and the same text (the text around the cursor, or as a second check the whole text the app gives for extraction, because some apps report the selection unreliably), and is never a password field. Otherwise the result is copied to the clipboard with a short message, instead of going into the wrong place. The same check is used when the keyboard stayed open.
  • No "Context is busy" (offline): the plugin can't stop a running generation, so a new request failed with "Context is busy" / "Model produced no output". Generations now run one after another.

The field check is a small class with unit tests (AiFieldCheckTest). In daily use in my fork since end of October.

🤖 Generated with Claude Code

@LeanBitLab LeanBitLab left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this. Keeping a custom key result when the editor hides the keyboard is useful, but the PR changes more than custom keys. A few regressions and risks before we can merge:

1. Proofread and translate are no longer cancelled when the keyboard hides (regression)
ProofreadHelper.cancelCurrentJob() is removed from both onWindowHidden and onFinishInputView. That also applies to proofread and translate, but their onSuccess still drops the result when the input view is hidden. So:

  • the request runs to the end for nothing (a paid cloud API call, or minutes of CPU on the offline model),
  • the loading border is still there when the keyboard comes back, possibly in another app,
  • the first tap on proofread in the new field only cancels the old request (with #890, translate and the custom keys do the same),
  • cancelCurrentJob() becomes dead code in both flavors.

Please keep the cancel on hide and skip it only while a custom key request runs. For example, ProofreadHelper could remember whether the running job was started by customAsync (cancelCurrentJob(keepCustomKeyJob = true)), so proofread and translate keep their current behaviour.

2. Typing during the request now sends the result to the clipboard (behaviour change with the keyboard open)
targetField.isCurrent() now also runs while the keyboard stays open. If the user keeps typing in the same field while waiting, the text no longer matches and the result goes to the clipboard. For #append this is a regression: before, the result was appended. Please keep the existing package/field id check for the normal path and use the text fingerprint only for a result that arrived while the keyboard was hidden (the case this PR is about).

3. Extra reads of the whole field on the main thread
currentAiTarget() reads up to 60k characters before and after the cursor plus getExtractedText (hint 120k chars) right after handleCustomAIKey has already read the same text, and again when the result arrives. In big fields or slow web editors that is several large IPC calls on the main thread. Suggestions:

  • build the request snapshot from the text already read for textToProcess,
  • only call getExtractedText when the first comparison fails.

4. A cancelled request still generates after waiting for generateLock (offline)
A request that was cancelled while it waited for the lock still runs a full generation once it gets the lock, because the plugin call can't be interrupted. Please check coroutineContext.isActive after taking the lock and skip provider.generate for a cancelled request.

5. Minor

  • mPendingAIResult should be cleared when a new AI request starts, so an old result can't be inserted after a newer one.
  • onFinishInputView runs on every field switch, so with (1) fixed this mostly goes away, but please test switching fields during a request in a chat app (same field id for every chat).

To keep #888 and #890 easy to merge, please keep the changes inside handleCustomAIKey, AiFieldCheck and ProofreadHelper, and leave the prompt building in ProofreadService (touched by #888) and the toolbar loading code in SuggestionStripView (touched by #890) as they are.

@LeanBitLab LeanBitLab left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Marking as changes requested: please address the regression and risks in my review comment above (mainly: keep cancelling proofread/translate when the keyboard hides, and keep the old insert behaviour while the keyboard stays open).

…ly into the same field

Some editors hide the keyboard while a custom AI key runs (they react to the
cursor being moved for the result). The request was cancelled and the result
lost. Now a custom key request keeps running when the keyboard hides
(proofread and translate are still cancelled), and a result that arrives
meanwhile is inserted when the keyboard is back in the same field within two
minutes.

Many apps use the same field id for all their text fields (e.g. every chat).
So a kept result, or a result after another field was opened meanwhile, is
only inserted if the app, input type and text of the field are the same as
when the request was made (from the text already read for the request; the
text the app gives for extraction is only read if the first comparison
fails), never in a password field. Otherwise it is copied to the clipboard
with a short message. While the keyboard stays in the same field, the
behaviour is as before.

Offline AI: one generation at a time. The plugin can't stop a running
generation, and a new request failed with "Context is busy". It now waits for
the previous one, and is skipped if it was cancelled while waiting.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VuiaMhLXwhErf9shhrr1uD
@4di-hello
4di-hello force-pushed the fix/ai-key-result-after-hide branch from 509bc75 to 583d1b3 Compare November 22, 2026 09:28
4di-hello added a commit to 4di-hello/LeanType that referenced this pull request Nov 22, 2026
- Proofread and translate are cancelled again when the keyboard hides (their
  result is dropped then anyway); only a custom key request keeps running.
- Appending with a custom key while typing on in the same field works again;
  the field check is used after another field was opened, and for replacing.
- A new custom key request drops an older kept result; a kept result is not
  inserted while a newer request runs.
- Offline: a request cancelled while waiting for the previous generation does
  not start one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VuiaMhLXwhErf9shhrr1uD
@4di-hello

Copy link
Copy Markdown
Contributor Author

Thanks for the careful review! All points are addressed in the updated branch (one commit, force-pushed):

  1. The cancel on hide is back. ProofreadHelper.cancelCurrentJob(keepCustomKeyJob = true) skips it only while a request started by customAsync runs; proofread and translate are cancelled as before, in both flavors.
  2. While the keyboard stays in the same field, the behaviour is as before (package/field id check only), so typing on during an #append request still appends. The text fingerprint is only used for a result that arrived while the keyboard was hidden, and when another field was opened during the request (counted in startInput) - the chat case with the same field id for every chat.
  3. No extra reads for the request: the fingerprint is built from textToProcess (the whole text without a selection; with a selection, a kept result goes to the clipboard), and only a hash and the length are stored. For the check, the text around the cursor is read once, getExtractedText only if that comparison fails.
  4. After taking generateLock, a request that was cancelled while waiting is skipped and doesn't start a generation.
  5. mPendingAIResult is cleared when a new custom key request starts, and a kept result isn't inserted while a newer request runs.

The changes stay in handleCustomAIKey and helpers next to it, two lines in startInput for the kept result, AiFieldCheck (with tests), ProofreadHelper, and the two cancel calls in LatinIME. The prompt building in ProofreadService and SuggestionStripView are untouched; the only ProofreadService change is the lock around provider.generate. The branch merges cleanly together with #888 and #890, and the unit tests pass in both flavors.

Tested on my phone: switching to another chat during a custom key request puts the result into the clipboard instead of the other chat, and appending while typing on in the same field works as before.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants